package com.ycwl.freetv.program;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ycwl.library.AssetsListManager;
import com.ycwl.library.ParseUIFromJson;

public class PlayListManger {
    private final static String TAG = "PlayListManger";
    private final static PlayListManger instance = new PlayListManger();
    public static PlayListManger getInstance() {
        return instance;
    }
    private int programIndex;

    private ParseUIFromJson.MPlayList mPlayList;
    public PlayListManger() {
        this.mPlayList = null;
        this.programIndex = 0;
    }

    public void init(String jsonStr) {
        Gson gson = new Gson();
        try {
            this.mPlayList = gson.fromJson(jsonStr, ParseUIFromJson.MPlayList.class);
            for (ParseUIFromJson.MProgram program: mPlayList.programs) {
                initRealData(program.view);
            }
        } catch (JsonSyntaxException e) {
            Log.e(TAG,"Init fail");
        }
    }

    private static void initRealData(ParseUIFromJson.BaseNode baseNode) {
        if (baseNode.type == ParseUIFromJson.NODE_TYPE.VIDEO || baseNode.type == ParseUIFromJson.NODE_TYPE.IMAGE) {
            AssetsListManager.Assets assets = AssetsListManager.getInstance().getAssetsByHash(baseNode.sha1);
            if (assets == null) {
                return;
            }
            baseNode.url = assets.path;
            assets.quote++;
        } else {
            for (ParseUIFromJson.BaseNode item :baseNode.children) {
                initRealData(item);
            }
        }
    }

    public long getUpdateTime() {
        if (this.mPlayList == null) {
            return 0;
        }

        return this.mPlayList.update_time.longValue();
    }

}
